from datetime import datetime
from utils.abstract_repository import AbstractRepository
from utils.models.flight import Flight
from utils.funcs import get_current_date


class FlightRepository(AbstractRepository[Flight]):
    model = Flight

    async def insert(self, user_id: int,
                     region_id: int, time_end: datetime) -> int:
        return await self.insert_one_with_data(
            user_id=user_id, region_id=region_id,
            time_end=get_current_date() + time_end)

    async def exists(self, user_id: int) -> bool:
        return await self.select_one_where(
                self.model.user_id == user_id) is not None

    async def delete_end_flights(self) -> None:
        await self.delete_where(self.model.time_end < get_current_date())
        return
